package com.example.aimnear;

import java.util.HashMap;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DbInterface {
	
	private SQLiteDatabase dataBase;
	DbInterfaceHelper helper;
	
	protected DbInterface (Context context) {
		this.helper = new DbInterfaceHelper(context); 
		 		
	}
	
	private void prepareBaseToUse () {
		this.dataBase = helper.getWritableDatabase();
	}
	
	public void closeBaseConnection () {
		this.helper.close();
	}
	
	public void addTask (String name, String description) {
		prepareBaseToUse();
		ContentValues cv = new ContentValues();
        cv.put(DbConstant.TASK_TABLE_NAME_COLUMN_NAME, name);
        cv.put(DbConstant.TASK_TABLE_DESCRIPTION_COLUMN_NAME, description);
        cv.put(DbConstant.TASK_TABLE_STATUS_COLUMN_NAME, DbConstant.TaskStatuses.OPEN.name());
        //i don' know why null too
        dataBase.insert(DbConstant.TASK_TABLE_NAME,null,cv);
	} 
	
	public void deleteTask (int id) {
		prepareBaseToUse();
		ContentValues values = new ContentValues();
		String where = DbConstant.TASK_TABLE_ID_COLUMN_NAME + " = ?";
		String [] whereParam =  new String [] {String.valueOf(id)};
		values.put(DbConstant.TASK_TABLE_STATUS_COLUMN_NAME, DbConstant.TaskStatuses.DELETED.name());
		dataBase.update(DbConstant.TASK_TABLE_NAME, values, where, whereParam);
	}
	
	public void completeTask (int id) {
		prepareBaseToUse();
		ContentValues values = new ContentValues();
		String where = DbConstant.TASK_TABLE_ID_COLUMN_NAME + " = ?";
		String [] whereParam =  new String [] {String.valueOf(id)};
	    values.put(DbConstant.TASK_TABLE_STATUS_COLUMN_NAME, DbConstant.TaskStatuses.CLOSED.name());
		dataBase.update(DbConstant.TASK_TABLE_NAME, values, where, whereParam);
	}
	
	public void clearTaskTable () {
		prepareBaseToUse();
		dataBase.delete(DbConstant.TASK_TABLE_NAME, null, null);
	}
	//TODO test required
	public void clearDeletedTasks () {
		prepareBaseToUse();
		String where = DbConstant.TASK_TABLE_STATUS_COLUMN_NAME + " = ?";
		String [] whereParam =  new String [] {DbConstant.TaskStatuses.DELETED.name()};
		dataBase.delete(DbConstant.TASK_TABLE_NAME, where, whereParam);
	}
	
	public Map<String,String> selectOpenTasks () {
		prepareBaseToUse();
		String[] columns = new String[] {DbConstant.TASK_TABLE_ID_COLUMN_NAME,DbConstant.TASK_TABLE_NAME_COLUMN_NAME};
	    String selection = DbConstant.TASK_TABLE_STATUS_COLUMN_NAME + " = ?";
	    String[] selectionArgs = new String[] {DbConstant.TaskStatuses.OPEN.name()};
	    String groupBy = null;
	    String having = null;
	    String orderBy = null;
	    
		Cursor cursor = dataBase.query(DbConstant.TASK_TABLE_NAME, columns, selection, selectionArgs, groupBy, having, orderBy);
		
		Map<String,String> rows = new HashMap<String,String>();
		if (cursor != null) {
			if (cursor.moveToFirst()) {
				do {
					rows.put(cursor.getString(cursor.getColumnIndex(DbConstant.TASK_TABLE_ID_COLUMN_NAME)),
							cursor.getString(cursor.getColumnIndex(DbConstant.TASK_TABLE_NAME_COLUMN_NAME)));
		        } while (cursor.moveToNext());
		    }
		    cursor.close();
		} 
		return rows;   
	}
	
	public Map<String,String> selectAllTasks () {
		prepareBaseToUse();
		String[] columns = new String[] {DbConstant.TASK_TABLE_ID_COLUMN_NAME,DbConstant.TASK_TABLE_NAME_COLUMN_NAME};
	    String selection = null;
	    String[] selectionArgs = null;
	    String groupBy = null;
	    String having = null;
	    String orderBy = null;
		
		Cursor cursor = dataBase.query(DbConstant.TASK_TABLE_NAME, columns, selection, selectionArgs, groupBy, having, orderBy);
		
		Map<String,String> rows = new HashMap<String,String>();
		if (cursor != null) {
			if (cursor.moveToFirst()) {
				do {
					//very easy
					rows.put(cursor.getString(cursor.getColumnIndex(DbConstant.TASK_TABLE_ID_COLUMN_NAME)),
							cursor.getString(cursor.getColumnIndex(DbConstant.TASK_TABLE_NAME_COLUMN_NAME)));
		        } while (cursor.moveToNext());
		    }
		    cursor.close();
		} 
		return rows;  
	}
	
}
